package com.tech.platform.cms.authz.sso.controller;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.jndi.toolkit.url.Uri;
import com.tech.platform.cms.authz.common.model.LoginUserModel;
import com.tech.platform.cms.authz.sso.dao.bean.TenantUser;
import com.tech.platform.cms.authz.sso.dao.mapper.TenantUserMapper;
import com.tech.platform.cms.authz.sso.service.JwtTokenService;
import com.tech.platform.cms.authz.sso.service.SsoAuthzService;
import com.tech.platform.cms.common.constant.Constant;
import com.tech.platform.cms.common.model.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(value = "/dy/sso")
public class SsoAuthzController {

    @Autowired
    private JwtTokenService jwtTokenService;

    @Autowired
    private SsoAuthzService ssoAuthzService;

    @ResponseBody
    @RequestMapping(value = "/hgt/check",method = RequestMethod.POST)
    public CommonResult<String> checkHgtToken(HttpServletRequest request){
        return ssoAuthzService.checkHgtToken(request);
    }

    @ResponseBody
    @RequestMapping(value = "/hgc/exchange",method = RequestMethod.POST)
    public CommonResult<String> exchangeHgtToken(HttpServletRequest httpServletRequest){
       return ssoAuthzService.exchangeHgtToken(httpServletRequest);
    }

    @RequestMapping(value = "/toLogin")
    public ModelAndView toLogin(HttpServletRequest request){
        return new ModelAndView("index");
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ModelAndView login(String tenantId,String username, String password, HttpServletRequest request, HttpServletResponse response) throws Exception{
//        LoginUserModel userModel = ssoAuthzService.login(username,password,tenantId);
        LoginUserModel userModel = new LoginUserModel().setUserId(1111).setUserName("123").setTenant("admin").setAvatar("http://img0.imgtn.bdimg.com/it/u=1950846641,3729028697&fm=11&gp=0.jpg");
        String redirectUrl = ssoAuthzService.makeCookieAndGetRedirectUrl(userModel,request,response);
        return new ModelAndView("redirect:"+redirectUrl);
    }
}
